<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 头部 -->
        <nodeData :node-data="headData" :isroot="true" @addDepartment="addDepartment" />
        <el-tree :data="treeData" :props="{label:'name'}" default-expand-all>
          <!-- 自定义插槽 -->
          <template #default="obj">
            <!-- 封装 -->
            <nodeData :node-data="obj.data" @addDepartment="addDepartment" @editDepartment="editDepartment" @DelDepartment="DelDepartment" />
          </template>``
        </el-tree>
      </el-card>
      <!-- 弹窗 -->
      <adddialog ref="addDept" :show-dialog.sync="showDialog" :node-data="nodeData" @addDepartmens="getTreeData" />
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import nodeData from './components/nodeData.vue'
import { getDeparmentList } from '@/api/department'
import { listtoTreeData } from '@/utils/index'
import adddialog from './components/adddialog.vue'
export default {
  name: 'Dashboard',
  components: { nodeData, adddialog },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  data() {
    return {
      showDialog: false,
      headData: {
        name: '江苏传智播客教育科技有限公司',
        manager: '负责人',
        id: ''
      },
      treeData: [],
      nodeData: {}

    }
  },
  async created() {
    this.getTreeData()
  },
  methods: {
    async  getTreeData() {
      const { depts } = await getDeparmentList()
      // console.log(await getDeparmentList())
      this.treeData = listtoTreeData(depts, '')
    },
    addDepartment(nodeData) {
      this.nodeData = nodeData
      // 弹窗
      this.showDialog = true
    },
    editDepartment(nodeData) {
      // 显示回显
      this.$refs.addDept.editDepartments(nodeData.id)
      this.nodeData = nodeData
      // 弹窗
      this.showDialog = true
    },
    DelDepartment() {
      this.getTreeData()
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
